package api

import (
	"errors"
	"github.com/gin-gonic/gin"
	"go-caipu/pkg/api/helper"
	"go-caipu/pkg/constant"
	dept2 "go-caipu/pkg/services/admin/dept"
	"go-caipu/pkg/services/admin/role"
	"go-caipu/pkg/util"
)

func (hs *HTTPServer) GetRolePage(c *gin.Context) {
	cmd := role.GetRolePageCommand{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}
	result, count, err := hs.roleService.GetRolePage(c.Request.Context(), cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithData(c, helper.Page{CurrentPage: cmd.PageIndex, PageSize: cmd.PageSize, List: result, Total: count})
}
func (hs *HTTPServer) GetRoleUser(c *gin.Context) {
	hs.GetUserPage(c)
}
func (hs *HTTPServer) GetRoleUserId(c *gin.Context) {
	roleId := c.Param("roleId")
	result, err := hs.roleService.GetUserIdsByRoleId(roleId)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithData(c, result)
}
func (hs *HTTPServer) GetRoleList(c *gin.Context) {
	cmd := role.GetRolePageCommand{}
	cmd.PageSize = 1000
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}
	result, _, err := hs.roleService.GetRolePage(c.Request.Context(), cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithData(c, result)

}

type roleOption struct {
	Label string `json:"label"`
	Value int64  `json:"value,string"`
}

func (hs *HTTPServer) GetRoleOptions(c *gin.Context) {
	cmd := role.GetRolePageCommand{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}
	cmd.Status = "1"
	result, _, err := hs.roleService.GetRolePage(c.Request.Context(), cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	items := make([]roleOption, len(result))
	for i := 0; i < len(result); i++ {
		items[i].Label = result[i].RoleName
		items[i].Value = result[i].RoleId
	}
	helper.SuccessWithData(c, items)
}

func (hs *HTTPServer) GetRole(c *gin.Context) {
	cmd := role.GetRoleCommand{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}

	result, err := hs.roleService.GetRole(c.Request.Context(), cmd)
	dataScope, err := hs.roleService.GetRoleDataScope(c.Request.Context(), result.RoleId)
	result.DeptIds = dataScope
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithData(c, result)
}

// GetRoleDataScope 暂时不使用【可删除】
func (hs *HTTPServer) GetRoleDataScope(c *gin.Context) {
	roleId := c.Param("roleId")
	if roleId == "" {
		c.JSON(constant.CodeErrBadRequest, "roleId is null")
		return
	}
	deptCmd := dept2.GetDeptTreeListCommand{}
	dept, err := hs.deptService.GetDeptTree(c.Request.Context(), deptCmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	//获取选择项
	checkedKeys, err := hs.roleService.GetRoleDataScope(c.Request.Context(), util.StringToInt64(roleId))

	helper.SuccessWithData(c, role.DataScopeResp{Dept: dept, CheckedKeys: checkedKeys})
}
func (hs *HTTPServer) CreateRole(c *gin.Context) {
	cmd := role.SysRoleInsertReq{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}

	cmd.Status = 1 //默认都可用
	cmd.RoleId = hs.Snowflake.Generate().Int64()
	err := hs.roleService.Insert(cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}

	helper.SuccessWithOutData(c)
}

func (hs *HTTPServer) UpdateRole(c *gin.Context) {
	cmd := role.SysRoleUpdateReq{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}

	err := hs.roleService.Update(cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithOutData(c)
}

// RemoveRole 删除角色
func (hs *HTTPServer) RemoveRole(c *gin.Context) {
	cmd := role.DelRoleCommand{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}

	err := hs.roleService.Remove(c.Request.Context(), cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}

	helper.SuccessWithOutData(c)
}
func (hs *HTTPServer) RemoveRoleUser(c *gin.Context) {
	roleId := c.Param("id")
	if roleId == "" {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, errors.New("roleId is null"))
		return
	}
	var userIds []string
	if err := c.ShouldBindJSON(&userIds); err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	err := hs.roleService.RemoveRoleUser(c.Request.Context(), roleId, userIds)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithOutData(c)
}

// UpdateRoleDataScope 暂时不使用【可删除】
func (hs *HTTPServer) UpdateRoleDataScope(c *gin.Context) {
	cmd := role.UpdateDataScopeCommand{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}

	err := hs.roleService.UpdateDataScope(c.Request.Context(), cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithOutData(c)
}

// UpdateRolePermission 暂时不使用【可删除】
func (hs *HTTPServer) UpdateRolePermission(c *gin.Context) {
	cmd := role.UpdateRolePermissionCommand{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}
	err := hs.roleService.UpdateRolePermission(c.Request.Context(), cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithOutData(c)
}

func (hs *HTTPServer) AddRoleUser(c *gin.Context) {
	roleId := c.Param("roleId")
	if roleId == "" {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, errors.New("roleId is null"))
		return
	}
	var userIds []string
	if err := c.ShouldBindJSON(&userIds); err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	err := hs.roleService.AddRoleUser(c.Request.Context(), roleId, userIds)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithOutData(c)
}
